package pub

import (
	"customer/api/handler"
	"customer/config"
	"customer/consts"
	"customer/inits"
	"customer/internal/model"
	"customer/internal/queue"
	"encoding/json"
	"log"
)

func Pub() {
	config.Init()
	inits.MysqlInit()
	inits.InitRabbitMq()

	msgs := queue.PublishSub(consts.SEND_MESSAGE)

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)

			var data model.SendMessage
			_ = json.Unmarshal(d.Body, &data)

			for _, conn := range handler.OnlineUser {
				handler.WSRespSuccess(conn, data.Content)
			}

		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
